package pl.edu.agh.mguesser.ui.game;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.PostConstruct;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.springframework.stereotype.Controller;
import pl.edu.agh.mguesser.game.answer.Answer;
import pl.edu.agh.mguesser.game.listener.AnswerRequiredListener;
import pl.edu.agh.mguesser.game.listener.GameStartedListener;

/**
 * @author keiran
 */
@Controller
public class GetAnswerPanel extends JPanel implements GameStartedListener, 
                                                      AnswerRequiredListener {

    private JButton guessedBtn;
    private JButton halfGuessedBtn;
    private JButton missedBtn;
    
    private volatile Answer answer;
    
    private Lock lock = new ReentrantLock();
    private Condition answerReady = lock.newCondition();
    
    @PostConstruct
    public void postConstruct() {
        setLayout(new FlowLayout());
        
        guessedBtn = new JButton("Guessed");
        halfGuessedBtn = new JButton("Half-Guessed");
        missedBtn = new JButton("Missed");
        
        guessedBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                lock.lock();
                try {
                    answer = Answer.GUESSED;
                    answerReady.signal();

                } finally {
                    lock.unlock();

                }
            }
        });
        halfGuessedBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                lock.lock();
                try {
                    answer = Answer.HALF_GUESSED;
                    answerReady.signal();

                } finally {
                    lock.unlock();

                }
            }
        });
        missedBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                lock.lock();
                try {
                    answer = Answer.MISSED;
                    answerReady.signal();

                } finally {
                    lock.unlock();

                }
            }
        });
        
        add(guessedBtn);
        add(halfGuessedBtn);
        add(missedBtn);
    }

    @Override
    public void gameStarted() {
        hideAllButtons();
    }

    @Override
    public Answer getAnswer() {
        Answer result = null;
        
        lock.lock();
        showAllButtons();
        
        answer = null;
        
        try {
            while(answer == null) {
                answerReady.await();
            }
            
            result = answer;
            
        } catch(InterruptedException ex) {
            // just ignore...
            
        } finally {
            answer = null;
        
            hideAllButtons();
            lock.unlock();
            
        }
        
        return result;
    }
    
    private void hideAllButtons() {
        guessedBtn.setVisible(false);
        halfGuessedBtn.setVisible(false);
        missedBtn.setVisible(false);
    }
    
    private void showAllButtons() {
        guessedBtn.setVisible(true);
        halfGuessedBtn.setVisible(true);
        missedBtn.setVisible(true);
    }

}
